'use strict';
app.service('consumerTopService',['$http','$q','utilService','$cookies',function($http,$q,utilService,$cookies){
    //导出数据
    this.exportData = function($scope){
        $http({
            method:'post',
            url:$scope.app.domain + 'report/consume/ranking',
            data:$scope.params,
            responseType: "arraybuffer",
            headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function(data){
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var fileName ="consumeRanking"+ (new Date()).getTime()+'.xls';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    };

    //导出用户消费数据
    this.userExportData = function($scope){
        $http({
            method:'post',
            url:$scope.app.domain + 'report/user/consume',
            data: {userId:$scope.param,sessionId:$cookies.sessionId},
            responseType: "arraybuffer",
            headers:{'Content-Type': 'application/x-www-form-urlencoded'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function(data){
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var fileName ="userConsume"+ (new Date()).getTime()+'.xls';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    }
}]);